package com.mingzhi.rabbitmq.config;

import com.mingzhi.rabbitmq.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /**
     * 创建一个名称为workQueue的可持久化队列
     *
     * @return
     */
    @Bean("workQueue")
    public Queue workQueue() {
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }

    /**
     * 创建一个名称为fanoutQueue1的可持久化队列
     *
     * @return
     */
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }

    /**
     * 创建一个名称为fanoutQueue2的可持久化队列
     *
     * @return
     */
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }

    /**
     * 创建一个名称为fanoutExchange的广播交换机
     *
     * @return
     */
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build();
    }

    /**
     * 将fanoutQueue1绑定到fanoutExchange交换机
     *
     * @return
     */
    @Bean
    public Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange,
                                  @Qualifier("fanoutQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

    /**
     * 将fanoutQueue2绑定到fanoutExchange交换机
     *
     * @return
     */
    @Bean
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange,
                                  @Qualifier("fanoutQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

    /**
     * 创建一个名称为directQueue1的可持久化队列
     *
     * @return
     */
    @Bean("directQueue1")
    public Queue directQueue1() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }

    /**
     * 创建一个名称为directQueue2的可持久化队列
     *
     * @return
     */
    @Bean("directQueue2")
    public Queue directQueue2() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }

    /**
     * 创建一个名称为directExchange的路由交换机
     *
     * @return
     */
    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
    }

    /**
     * 将directQueue1绑定到directExchange交换机 路由键为orange
     *
     * @return
     */
    @Bean
    public Binding directBinding1(@Qualifier("directExchange") DirectExchange directExchange,
                                  @Qualifier("directQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("orange");
    }

    /**
     * 将directQueue2绑定到directExchange交换机 路由键为black
     *
     * @return
     */
    @Bean
    public Binding directBinding2(@Qualifier("directExchange") DirectExchange directExchange,
                                  @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("black");
    }

    /**
     * 将directQueue2绑定到directExchange交换机 路由键为orange
     *
     * @return
     */
    @Bean
    public Binding directBinding3(@Qualifier("directExchange") DirectExchange directExchange,
                                  @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("orange");
    }

    /**
     * 创建一个名称为topicQueue1的可持久化队列
     *
     * @return
     */
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }

    /**
     * 创建一个名称为topicQueue2的可持久化队列
     *
     * @return
     */
    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }

    /**
     * 创建一个名称为topicExchange的topic交换机
     *
     * @return
     */
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * 将topicQueue1绑定到topicExchange交换机 并指定 routingKey 为 *.orange.*
     *
     * @return
     */
    @Bean
    public Binding topicBinding1(@Qualifier("topicExchange") TopicExchange topicExchange,
                                 @Qualifier("topicQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("*.orange.*");
    }

    /**
     * 将topicQueue2绑定到topicExchange交换机 并指定 routingKey 为 *.*.rabbit
     *
     * @return
     */
    @Bean
    public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange topicExchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("*.*.rabbit");
    }

    /**
     * 将topicQueue2绑定到topicExchange交换机 并指定 routingKey 为 lazy.#
     *
     * @return
     */
    @Bean
    public Binding topicBinding3(@Qualifier("topicExchange") TopicExchange topicExchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("lazy.#");
    }


}
